const Router = require('koa-router')
const router = new Router({
    prefix: '/cms/good'
});
// const { IntValidator }  = require('../../validators/validator')

const { GoodClass } = require('../../models/good/good_class')
const { GoodSpec } = require('../../models/good/good_spec')
const { Goods } = require('../../models/good/goods')


/**
 * 商品规格表
 */

//批量添加商品规格
router.post('/spec/add', async ctx => {
    const body = ctx.request.body;

    //先删除
    const where = { product_id: body[0].product_id }
    await GoodSpec.destroy({ where, force: true })

    //再添加
    const res = await GoodSpec.bulkCreate(body)
    ctx.body = {
        data: res
    }
})

// 查询商品规格表
router.post('/spec/page', async ctx => {
    const body = ctx.request.body;
    const { product_id } = body;
    const where = { product_id }
    const res = await GoodSpec.findAll({ where })
    ctx.body = {
        data: res
    }
})
// 更新商品规格
router.post('/spec/update', async ctx => {
    const body = ctx.request.body;

    const res = await GoodSpec.update(body, { where: { id: body.id } })
    ctx.body = {
        data: res
    }
})
// 删除单个商品整个的规格表
router.post('/spec/delete', async ctx => {
    const body = ctx.request.body;
    const { product_id } = body;
    const where = { product_id }
    const res = await GoodSpec.destroy({ where, force: true })
    ctx.body = {
        data: res
    }
})
// 删除单个商品某一个的规格表
router.post('/spec/delete_one', async ctx => {
    const body = ctx.request.body;
    const { id } = body;
    const where = { id }
    const res = await GoodSpec.destroy({ where, force: true })
    ctx.body = {
        data: res
    }
})

/**
 * 商品接口
 */
router.post('/add', async ctx => {
    const body = ctx.request.body;

    const res = await Goods.create(body);

    ctx.body = {
        data: res
    }
})
/**
 * 根据商品ID修改商品
 */
router.post('/update_single', async (ctx, next) => {

    const body = ctx.request.body;
    const { id } = body;
    const res = await Goods.update(body,{
        where: { id },
    })
    ctx.body = {
        data: res
    };
})

//更新商品
router.post('/update', async ctx => {
    const body = ctx.request.body;
    const { type, data } = body;
    if (type == 'attr_list') {
        await Goods.update({ attr_list: data.attr_list }, {
            where: {
                id: data.id
            }
        })
    } else {

    }

    ctx.body = {
        data: null,
        msg: '更新成功'
    }
})

//查询某一类的商品
router.post('/page', async ctx => {
    const body = ctx.request.body;
    const { class_id } = body;
    const res = await Goods.findAll({
        where: { class_id }
    });

    ctx.body = {
        data: res
    }
})




/**
 * 分类接口
 */

//更改分类
router.post('/class/edit', async ctx => {
    const body = ctx.request.body;

    const { id } = body;

    await GoodClass.update(body, {
        where: {
            id
        }
    })

    ctx.body = {
        msg: '修改成功'
    }

})


//删除
router.post('/class/delete', async (ctx, next) => {
    const body = ctx.request.body;

    //递归删除
    const destroyAll = async id => {
        await GoodClass.destroy({
            where: {
                id: id
            }
        })

        let childs = await GoodClass.findAll({
            where: {
                parent_id: id
            }
        })

        if (childs.length) {
            for (let i of childs) {
                await destroyAll(i.id)
            }
        }

    }

    destroyAll(body.id);

    ctx.body = {
        data: 'ok'
    };

})
//新增
router.post('/class/add', async (ctx, next) => {
    const body = ctx.request.body;

    body.status = 1;

    const res = await GoodClass.create(body)

    ctx.body = {
        data: res
    };
})

//查询所有分类 并且输出树形json
router.get('/class', async (ctx, next) => {

    let res = await GoodClass.findAll({
        where: {
            parent_id: 0
        }
    })


    //递归查询子节点
    const getTree = async res => {
        res = JSON.parse(JSON.stringify(res));

        let childPromise = [];

        res.forEach(item => {
            childPromise.push(GoodClass.findAll({
                where: {
                    parent_id: item.id
                }
            }))
        });

        const child = await Promise.all(childPromise);

        for (let i in child) {
            if (child[i].length > 0) {
                child[i] = await getTree(child[i])
            }
            res[i].children = child[i]
        }

        return res;

    }




    let result = await getTree(res)

    ctx.body = {
        data: result
    }
})


module.exports = router;